local sz_tuishen = fk.CreateSkill {
    name = "sz_tuishen",
    tags = { Skill.Quest },
}

Fk:loadTranslationTable {
    ["sz_tuishen"] = "蜕身",
    [":sz_tuishen"] = "使命技，当你于摸牌阶段外获得牌时，你可以视为使用一张本局游戏已使用过的非装备牌，无距离和次数限制(每种牌名限一次，使命达成后，此技能不失效)。<br>\
  ⬤　成功：当你使用〖蜕身〗牌时，若你的体力上限不小于8，回复体力至上限，失去〖幻瘾〗，获得〖化蝶〗，重置〖蜕身〗。<br>\
  ⬤　失败：当你进入濒死状态时，你调整体力和体力上限至游戏开始时，修改〖幻瘾〗。",

    ["@$sz_tuishen_canuse"] = "蜕身",

    ["$sz_tuishen1"] = "使命成功",
    ["$sz_tuishen2"] = "使命成功",

}

sz_tuishen:addEffect(fk.AfterCardsMove, {
    anim_type = "support",
    can_trigger = function(self, event, target, player, data)
        if player:hasSkill(sz_tuishen.name) and player.phase ~= Player.Draw and #player:getTableMark("@$sz_tuishen_canuse") > 0 then
            for _, move in ipairs(data) do
                if move.to == player and move.toArea == Player.Hand and move.skillName ~= sz_tuishen.name then
                    return true
                end
            end
        end
    end,
    on_cost = function(self, event, target, player, data)
        local room = player.room
        local markyes = player:getTableMark("@$sz_tuishen_canuse")
        local use = room:askToUseVirtualCard(player, {
            name = markyes,
            skill_name = sz_tuishen.name,
            prompt = "蜕身：请选择要使用的牌",
            cancelable = true,
            extra_data = {
                bypass_times = true,
                extraUse = true,
            },
            skip = true,
        })
        if use then
            event:setCostData(self, { extra_data = use })
            return true
        end
    end,
    on_use = function(self, event, target, player, data)
        player.room:useCard(event:getCostData(self).extra_data)
    end,
})

sz_tuishen:addEffect(fk.CardUseFinished, {
    can_refresh = function(self, event, target, player, data)
        local markno = player:getTableMark("sz_tuishen_notuse")
        local markyes = player:getTableMark("@$sz_tuishen_canuse")
        return player:hasSkill(sz_tuishen.name, true) and data.card.type ~= Card.TypeEquip and
            not table.contains(markno, data.card.trueName) and not table.contains(markyes, data.card.trueName) and
            data.card.sub_type ~= Card.SubtypeDelayedTrick
    end,
    on_refresh = function(self, event, target, player, data)
        player.room:addTableMark(player, "@$sz_tuishen_canuse", data.card.trueName)
    end,
})

sz_tuishen:addEffect(fk.CardUsing, {
    mute = true,
    is_delay_effect = true,
    can_trigger = function(self, event, target, player, data)
        local markno = player:getTableMark("sz_tuishen_notuse")
        local markyes = player:getTableMark("@$sz_tuishen_canuse")
        return player:hasSkill(sz_tuishen.name, true) and data.card.type ~= Card.TypeEquip and
            table.contains(data.card.skillNames, sz_tuishen.name) and table.contains(markyes, data.card.trueName) and
            not table.contains(markno, data.card.trueName) and
            data.card.sub_type ~= Card.SubtypeDelayedTrick
    end,
    on_cost = Util.TrueFunc,
    on_use = function(self, event, target, player, data)
        local room = player.room
        room:removeTableMark(player, "@$sz_tuishen_canuse", data.card.trueName)
        room:addTableMark(player, "sz_tuishen_notuse", data.card.trueName)
        if player.maxHp >= 8 and player:getMark("tuishen_used") == 0 then
            room:setPlayerMark(player, "tuishen_used", 1)
            room:notifySkillInvoked(player, "sz_tuishen","big")
            room:updateQuestSkillState(player, sz_tuishen.name, false) --false是成功，迷惑
            if player:isWounded() then
                room:recover {
                    who = player,
                    num = player.maxHp - player.hp,
                    recoverBy = player,
                    skillName = sz_tuishen.name,
                }
            end
            room:handleAddLoseSkills(player, "-sz_huanyin")
            room:handleAddLoseSkills(player, "sz_huadie")
            for _, id in ipairs(player:getTableMark("sz_tuishen_notuse")) do
                local card = Fk:cloneCard(id)
                room:addTableMark(player, "@$sz_tuishen_canuse", id)
            end
            room:setPlayerMark(player, "sz_tuishen_notuse", 0)
        end
    end,
})
sz_tuishen:addEffect(fk.EnterDying, {
    mute = true,
    can_trigger = function(self, event, target, player, data)
        return target == player and player:hasSkill(sz_tuishen.name) and player:getMark("tuishen_used") == 0
    end,
    on_cost = Util.TrueFunc,
    on_use = function(self, event, target, player, data)
        local room = player.room
        room:setPlayerMark(player, "tuishen_used", 1)
        local n = player:getMark("playermaxhp")
        local x = player:getMark("playerhp")
        room:notifySkillInvoked(player, sz_tuishen.name, "offensive")
        room:updateQuestSkillState(player, sz_tuishen.name, true)
        if player.maxHp > n then
            room:changeMaxHp(player, -math.abs(player.maxHp - n))
        elseif player.maxHp < n then
            room:changeMaxHp(player, math.abs(player.maxHp - n))
        end
        if player.hp > x then
            room:changeHp(player, -math.abs(x - player.hp), nil, sz_tuishen.name)
        elseif player.hp < x then
            room:changeHp(player, math.abs(x - player.hp), nil, sz_tuishen.name)
        end
        room:handleAddLoseSkills(player, "-sz_huanyin")
        room:handleAddLoseSkills(player, "sz_huanyin_ex")
    end,
})
sz_tuishen:addEffect(fk.GameStart, {
    can_refresh = function(self, event, target, player, data)
        return player:hasSkill(sz_tuishen.name)
    end,
    on_refresh = function(self, event, target, player, data)
        local room = player.room
        room:setPlayerMark(player, "playerhp", player.hp)
        room:setPlayerMark(player, "playermaxhp", player.maxHp)
    end,
})
return sz_tuishen
